/*******************************************************************************
 * Simple C-K Editor
 * 
 * Copyright (C) 2009 Ingi BROWN - Centre de Gestion Scientifique - Ecole des Mines ParisTech
 * Licensed under the GNU General Public License Version 3.
 * 
 * Simple C-K Editor includes source code and libraries from the JUNG Project (Java Universal Network/Graph Framework) 
 * released under the BSD license. Copyright (c) 2003-2004,  Regents of the University of California and the JUNG Project.
 * 
 * See the License.txt file for more details.
 ******************************************************************************/

/*
 * EdgePropertyDialog.java
 *
 * Created on March 22, 2007, 2:23 PM
 * Copyright 2007 Grotto Networking
 */

package edu.ensmp.ckEditor.mousePlugin;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;

import edu.ensmp.ckEditor.ckModel.CKModel.Concept;
import edu.ensmp.ckEditor.ckModel.CKModel.Knowledge;
import edu.ensmp.ckEditor.main.Launcher;


/**
 * @author Dr. Greg M. Bernstein
 * @author Modified by Ingi BROWN (09/2009)
 */
@SuppressWarnings("serial")
public class VertexPropertyDialog extends JDialog {

	/** Creates new form EdgePropertyDialog */
	public VertexPropertyDialog(Frame parent, final Concept concept) {
		super(parent, true);
		setTitle(concept.toString());

		setLayout(new FlowLayout());

		final JTextField index = new JTextField(String.valueOf(concept
				.getIndex()), 4);
		add(index);

		final JTextField proposition = new JTextField(concept.getProposition(),
				40);
		proposition.selectAll();
		add(proposition);

		JButton okButton = new JButton("Change");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				okButtonHandler(evt, concept, proposition.getText(), Integer
						.parseInt(index.getText()));
			}
		});

		add(okButton);
		pack();

	}

	public VertexPropertyDialog(Frame parent, final Knowledge knowledge) {
		super(parent, true);
		setTitle(knowledge.toString());

		setLayout(new FlowLayout());

		final JTextField index = new JTextField(String.valueOf(knowledge
				.getIndex()), 4);
		add(index);

		final JTextField proposition = new JTextField(knowledge
				.getProposition(), 40);
		proposition.selectAll();
		add(proposition);

		JButton okButton = new JButton("Change");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				okButtonHandler(evt, knowledge, proposition.getText(), Integer
						.parseInt(index.getText()));
			}
		});

		add(okButton);
		pack();
	}

	protected void okButtonHandler(ActionEvent evt, Concept concept,
			String proposition, int index) {
		Launcher.getLogger().addLine(
				"Renaming Concept: [" + concept.toString() + "] => [C" + index
						+ proposition + "]");
		concept.setProposition(proposition);
		concept.setIndex(index);
		Launcher.getEditorGUI().getEditorFrame().pack();
		dispose();
	}

	protected void okButtonHandler(ActionEvent evt, Knowledge knowledge,
			String proposition, int index) {
		Launcher.getLogger().addLine(
				"Renaming Knowledge: [" + knowledge.toString() + "] => [K"
						+ index + proposition + "]");
		knowledge.setProposition(proposition);
		knowledge.setIndex(index);
		Launcher.getEditorGUI().getEditorFrame().pack();
		dispose();
	}
}
